/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import InneractiveSDK.IADView;
import java.util.Vector;
import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import util.ImageHelper;

/**
 *
 * @author Thinh
 */
public class Splash extends GamePage {
    public static final String TEXT_WAITING = "Tap to continue!";
    
    private static final int SCREEN_NONE = 0;
    private static final int SCREEN_OPENITVN = 1;
    private static final int SCREEN_SPONSOR = 2;
    private static final int SCREEN_SPLASH = 3;
    
    private boolean showWaitingText = false;
    private int waiting = 0;
    private int currentScreen = SCREEN_NONE;
    private Image backgroundTexture;
    private Vector ads;
    
    private Main parent;
    
    public Splash(Main _parent) {
        super();
        parent = _parent;
        
        schedule = 2000;
        new Thread(this).start();
    }
    
    public void setLoadingStatus(boolean status) {
        isLoading = status;
    }
    
    protected void update(){
        if(currentScreen == SCREEN_SPLASH) showWaitingText = !showWaitingText;
        else if(currentScreen != SCREEN_SPONSOR) nextScreen();
        
        if(waiting > 0) {
            waiting--;
            if(waiting == 0) {
                schedule = 2000;
                nextScreen();
            }
        }
    }
    
    public void paint(Graphics g) {
        if(isLoading) {
            g.fillRect(0, 0, Main.SCREENSIZE_WIDTH, Main.SCREENSIZE_HEIGHT);
            if(currentScreen == SCREEN_SPONSOR) {
                g.fillRect(0, 0, Main.SCREENSIZE_WIDTH, Main.SCREENSIZE_HEIGHT);
                g.setColor(255, 255, 255);
                g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL));
                g.drawString("Tap on a banner and all avertisments", Main.SCREENSIZE_WIDTH / 2, Main.SCREENSIZE_HEIGHT - 38, Graphics.HCENTER | Graphics.BASELINE);
                g.drawString("will be disabled for next 24 hours!", Main.SCREENSIZE_WIDTH / 2, Main.SCREENSIZE_HEIGHT - 24, Graphics.HCENTER | Graphics.BASELINE);
                g.drawString("Or click anywhere to skip.", Main.SCREENSIZE_WIDTH / 2, Main.SCREENSIZE_HEIGHT - 10, Graphics.HCENTER | Graphics.BASELINE);
                g.drawString("fetching banner...", Main.SCREENSIZE_WIDTH / 2, Main.SCREENSIZE_HEIGHT / 2, Graphics.HCENTER | Graphics.BASELINE);
                g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE));
                g.drawString("Sponsor Page", Main.SCREENSIZE_WIDTH / 2, 24, Graphics.HCENTER | Graphics.BASELINE);
            }
            return;
        }
        
        if(currentScreen == SCREEN_SPONSOR) {
            //trang quảng cáo
            g.fillRect(0, 0, Main.SCREENSIZE_WIDTH, Main.SCREENSIZE_HEIGHT);
            g.setColor(255, 255, 255);
            g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL));
            g.drawString("Tap on a banner and all avertisments", Main.SCREENSIZE_WIDTH / 2, Main.SCREENSIZE_HEIGHT - 38, Graphics.HCENTER | Graphics.BASELINE);
            g.drawString("will be disabled for next 24 hours!", Main.SCREENSIZE_WIDTH / 2, Main.SCREENSIZE_HEIGHT - 24, Graphics.HCENTER | Graphics.BASELINE);
            g.drawString("Or click anywhere to skip.", Main.SCREENSIZE_WIDTH / 2, Main.SCREENSIZE_HEIGHT - 10, Graphics.HCENTER | Graphics.BASELINE);
            if(ads != null) {
                g.drawImage((Image)ads.elementAt(0), Main.SCREENSIZE_WIDTH / 2, Main.SCREENSIZE_HEIGHT / 2, Graphics.HCENTER | Graphics.VCENTER);
            } else {
                g.drawString("Connection failed!", Main.SCREENSIZE_WIDTH / 2, Main.SCREENSIZE_HEIGHT / 2 - 10, Graphics.HCENTER | Graphics.BASELINE);
                g.drawString("Please wait for " + waiting + " seconds...", Main.SCREENSIZE_WIDTH / 2, Main.SCREENSIZE_HEIGHT / 2 + 10, Graphics.HCENTER | Graphics.BASELINE);
            }
            g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE));
            g.drawString("Sponsor Page", Main.SCREENSIZE_WIDTH / 2, 24, Graphics.HCENTER | Graphics.BASELINE);
        } else {
            //trang intro
            g.drawImage(backgroundTexture, 0, 0, Graphics.TOP | Graphics.LEFT);
            if(currentScreen == SCREEN_SPLASH && showWaitingText) {
                g.setColor(0, 0, 0);
                g.drawString(TEXT_WAITING, Main.SCREENSIZE_WIDTH / 2 + 2, Main.SCREENSIZE_HEIGHT - 16 + 2, Graphics.HCENTER | Graphics.BASELINE);
                g.setColor(255, 255, 255);
                g.drawString(TEXT_WAITING, Main.SCREENSIZE_WIDTH / 2, Main.SCREENSIZE_HEIGHT - 16, Graphics.HCENTER | Graphics.BASELINE);
            }
        }
    }
    
    protected void pointerPressed(int x, int y) {
        if(currentScreen == SCREEN_SPONSOR && ads != null) {
            int imgX1 = (Main.SCREENSIZE_WIDTH - ((Image)ads.elementAt(0)).getWidth()) / 2;
            int imgY1 = (Main.SCREENSIZE_HEIGHT - ((Image)ads.elementAt(0)).getHeight()) / 2;
            int imgX2 = Main.SCREENSIZE_WIDTH - imgX1;
            int imgY2 = Main.SCREENSIZE_HEIGHT - imgY1;
            if(x > imgX1 && x < imgX2 && y > imgY1 && y < imgY2) {
                //nếu bấm vào quảng cáo
                try {
                    parent.bannerPressed();
                    parent.platformRequest((String)ads.elementAt(1));
                } catch (ConnectionNotFoundException ex) {}
            }
            nextScreen();
        } else if(currentScreen == SCREEN_SPLASH) {
            parent.gotoMainMenu();
        }
    }
    
    public void dispose() {
        isLoading = true;
        pageLooping = false;
        backgroundTexture = null;
        ads = null;
    }
    
    private void nextScreen() {
        isLoading = true;
        currentScreen++;
        switch(currentScreen) {
            case SCREEN_OPENITVN:
                backgroundTexture = ImageHelper.loadImage("/images/logoopenitvn.png");
                break;
                
            case SCREEN_SPONSOR:
                if(parent.displayAds) {
                    backgroundTexture = null;
                    ads = IADView.getBannerAdData(parent, Main.NAX_CODE);
                    if(ads == null) {
                        waiting = 30;
                        schedule = 1000;
                    }
                } else {
                    nextScreen();
                }
                break;
                
            case SCREEN_SPLASH:
                ads = null;
                backgroundTexture = ImageHelper.loadImage("/images/splash.png");
                schedule = 500;
                break;
        }
        isLoading = false;
    }
}
